<?php

function cp($time,$rid){
	//$time=strtotime(date('Y-m-d H:00:00')); //'2022-01-01 12:00:00
	$characters = str_split(md5($time+$rid));
	$vv="";
	$a="";
	$b="";
	$c="";
	$d="";
	foreach($characters as $k=>$v){
		  if(ctype_digit($v)){
			  $vv .=$v;
		  }
	}
	$nv= substr($vv,1,5);
	//判断下够数步？
	if(strlen($nv) != 6){
		 $length=strlen($nv);
		
		 if($length<6){
			  $nv=str_pad($nv, 6, "0", STR_PAD_RIGHT);
		  }
	}
   
	$jz= dechex($nv);

   
   
		 $all = strlen($jz);
		 if($all==3){
		   $a= substr($jz, -2,2);
		   $b= "0".substr($jz, -3,1);
		   $c= "00";
		   $d= "00";
		 }
		 if($all==4){
		   $a= substr($jz, -2,2);
		   $b= substr($jz, -4,2);
		   $c= "00";
		   $d= "00";
		 }
		 if($all==5){
		   $a= substr($jz, -2,2);
		   $b= substr($jz, -4,2);
		   $c= "0".substr($jz, -5,1);
		   $d= "00";
		 }
		 if($all==6){
		   $a= substr($jz, -2,2);
		   $b= substr($jz, -4,2);
		   $c= substr($jz, -6,2);
		   $d= "00";
		 }
		 if($all==7){
		   $a= substr($jz, -2,2);
		   $b= substr($jz, -4,2);
		   $c= substr($jz, -6,2);
		   $d= "0".substr($jz, -7,1);
		 }
		 if($all==8){
		   $a= substr($jz, -2,2);
		   $b= substr($jz, -4,2);
		   $c= substr($jz, -6,2);
		   $d= substr($jz, -8,2);
		 }
		// var_dump($nv);
	    //生成密码178C0000815F180F01000000B4940700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
		//178C0000815F180F01000000 B4940700 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
	   $password=strtoupper($a).strtoupper($b).strtoupper($c).strtoupper($d);
	//   var_dump($password);
	  return $password;
   }
 function createPassword(){
   $time=strtotime(date('Y-m-d H:00:00'));
   $passwora=cp($time,1);
   $passworb=cp($time,2);
   $passworc=cp($time,3);
   $password=cp($time,4);
   $passwore=cp($time,5);
   $passworf=cp($time,6);
   $passworg=cp($time,7);
   
   // var_dump($nv,$jz,strlen($jz),$a,$b,$c,$d,$password);
   //更新密码
   setPassword(1,$passwora);
   setPassword(2,$passworb);
   setPassword(3,$passworc);
   setPassword(4,$password);
   setPassword(5,$passwore);
   setPassword(6,$passworf);
   setPassword(7,$passworg);
  
   return;
}

function  setPassword($open,$password){

       

$ip ='61.169.85.252';
$port ='60001';//60001  一号控制器   60002 二号控制器

if($open==1){
   //大门  同一个标号  控制器2  4号门
   $port='60002';
   $prodocunt="174000005665021B04000000".$password."000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
}
if($open==2 ){
   //衣柜1 控制器1 01号门
   $port='60001';
   $prodocunt="174000005F65021B01000000".$password."000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
}
if($open==3){
   //衣柜2 控制器1 02号门
   $port='60001';
   $prodocunt="174000005F65021B02000000".$password."000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
}
if($open==4){
   //衣柜3 控制器2 03号门
   $port='60002';
   $prodocunt="174000005665021B03000000".$password."000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
}
if($open==5){
   //淋浴间1 控制器2 01号门
   $port='60002';
   $prodocunt="174000005665021B01000000".$password."000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
}
if($open==6){
   //淋浴间2 控制器2 02号门
   $port='60002';
   $prodocunt="174000005665021B02000000".$password."000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
}
if($open==7){
   //淋浴间3 控制器1 03号门
   $port='60001';
   $prodocunt="174000005F65021B03000000".$password."000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
}
//创建连接
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//连接server
$ret = socket_connect($socket, $ip, $port);
//发送消息
socket_write($socket, hex2bin($prodocunt));
//读取消息
//	$buffer = socket_read($socket, 1024);
 //关闭连接
socket_close($socket);
return true;

}

createPassword();
?>